import BasePresenter from "../../../../../../redare/presenter/BasePresenter";
import * as PermissionHttp from "../../../http/component/core/PermissionHttp";

/**
 * Created by RdevPlugins.JsPresenterBuilderAction on 2020-03-07 13:02:40.
 */

export default class PermissionPresenter extends BasePresenter {

    /**
     * @param body: {roleId, funIds, platform, userPlatform}
     * @param query: {}
     * @returns Promise
     */
    addRoleFun(body, query, success, fails) {

        if (this._onBeforeAddRoleFun) {
            let data = this._onBeforeAddRoleFun(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PermissionHttp.addRoleFun(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeAddRoleFunSuccess) {
                    result = this._onBeforeAddRoleFunSuccess(result, {body, query});
                }

                if (this.view && this.view._onAddRoleFunSuccess) {
                    this.view._onAddRoleFunSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {roleId, menuIds, platform, userPlatform}
     * @param query: {}
     * @returns Promise
     */
    addRoleMenu(body, query, success, fails) {

        if (this._onBeforeAddRoleMenu) {
            let data = this._onBeforeAddRoleMenu(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PermissionHttp.addRoleMenu(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeAddRoleMenuSuccess) {
                    result = this._onBeforeAddRoleMenuSuccess(result, {body, query});
                }

                if (this.view && this.view._onAddRoleMenuSuccess) {
                    this.view._onAddRoleMenuSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {roleId, funIds, platform, userPlatform}
     * @param query: {}
     * @returns Promise
     */
    delRoleFun(body, query, success, fails) {

        if (this._onBeforeDelRoleFun) {
            let data = this._onBeforeDelRoleFun(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PermissionHttp.delRoleFun(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeDelRoleFunSuccess) {
                    result = this._onBeforeDelRoleFunSuccess(result, {body, query});
                }

                if (this.view && this.view._onDelRoleFunSuccess) {
                    this.view._onDelRoleFunSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {roleId, menuIds, platform, userPlatform}
     * @param query: {}
     * @returns Promise
     */
    delRoleMenu(body, query, success, fails) {

        if (this._onBeforeDelRoleMenu) {
            let data = this._onBeforeDelRoleMenu(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PermissionHttp.delRoleMenu(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeDelRoleMenuSuccess) {
                    result = this._onBeforeDelRoleMenuSuccess(result, {body, query});
                }

                if (this.view && this.view._onDelRoleMenuSuccess) {
                    this.view._onDelRoleMenuSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {roleId, componentId, id, module, classifyName, classifyNameLevel, key, platform, userPlatform}
     * @param query: {curPage,pageSize}
     * @returns Promise
     */
    findRoleFunPage(body, query, success, fails) {

        if (this._onBeforeFindRoleFunPage) {
            let data = this._onBeforeFindRoleFunPage(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PermissionHttp.findRoleFunPage(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeFindRoleFunPageSuccess) {
                    result = this._onBeforeFindRoleFunPageSuccess(result, {body, query});
                }

                if (this.view && this.view._onFindRoleFunPageSuccess) {
                    this.view._onFindRoleFunPageSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {roleId, menuId, id, module, classifyName, classifyNameLevel, key, platform, userPlatform}
     * @param query: {returnAll,curPage,pageSize}
     * @returns Promise
     */
    findRoleFunList(body, query, success, fails) {

        if (this._onBeforeFindRoleFunList) {
            let data = this._onBeforeFindRoleFunList(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PermissionHttp.findRoleFunList(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeFindRoleFunListSuccess) {
                    result = this._onBeforeFindRoleFunListSuccess(result, {body, query});
                }

                if (this.view && this.view._onFindRoleFunListSuccess) {
                    this.view._onFindRoleFunListSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {roleId, menuId, platformMenuId, parentMenuId, enabled, excRoot, sortToTree, levelTreeQuery, platform}
     * @param query: {curPage,pageSize}
     * @returns Promise
     */
    findRoleMenuPage(body, query, success, fails) {

        if (this._onBeforeFindRoleMenuPage) {
            let data = this._onBeforeFindRoleMenuPage(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PermissionHttp.findRoleMenuPage(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeFindRoleMenuPageSuccess) {
                    result = this._onBeforeFindRoleMenuPageSuccess(result, {body, query});
                }

                if (this.view && this.view._onFindRoleMenuPageSuccess) {
                    this.view._onFindRoleMenuPageSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {roleId, menuId, platformMenuId, parentMenuId, enabled, excRoot, sortToTree, levelTreeQuery, platform}
     * @param query: {returnAll,curPage,pageSize}
     * @returns Promise
     */
    findRoleMenuList(body, query, success, fails) {

        if (this._onBeforeFindRoleMenuList) {
            let data = this._onBeforeFindRoleMenuList(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PermissionHttp.findRoleMenuList(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeFindRoleMenuListSuccess) {
                    result = this._onBeforeFindRoleMenuListSuccess(result, {body, query});
                }

                if (this.view && this.view._onFindRoleMenuListSuccess) {
                    this.view._onFindRoleMenuListSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

}
